#include <iostream>
#include <cstdio>
using namespace std;
int m,n,a[530],maxs=0,s=0,mins=1001;
bool bz[530];
int main ()
{
	freopen("change.in","r",stdin);
	freopen("change.out","w",stdout);
	cin>>m>>n;
	for (int i=1;i<=n;i++)
		cin>>a[i];
	for (int t=1;t<=m;t++)
	{
		maxs=0,mins=1001;
		int k;
		for (int i=1;i<=n;i++)
			if (a[i]>maxs&&bz[i]!=1)
				maxs=a[i],k=i;
		bz[k]=1;
		k=0;
		for (int i=1;i<=n;i++)
			if (a[i]<mins&&bz[i]!=1)
				mins=a[i],k=i;
		bz[k]=1;
		if (mins==1001)
			mins=0;
		s+=maxs-mins;
	}
	cout<<s;
	return 0;
}
